blog

Home / DeveloperSection / Blogs / Set Default Value to Property in C#

Set Default Value to Property in C#

Set Default Value to Property in C#

Ravi Vishwakarma 2797 10-Jun-2024

In C#, you can set default values to effects in several ways, trusting the context and the type of the property. 

Here are a few common methods to set default values to properties in a class:

1. Default Values in Field Initializers

You can initialize the property directly where it's declared.

public class Person
{
   public string Name { get; set; } = "Unknown";
   public int Age { get; set; } = 0;
   public bool IsStudent { get; set; } = false;
}

2. Default Values in Constructors

You can set default values in the constructor of the class.

public class Person
{
   public string Name { get; set; }
   public int Age { get; set; }
   public bool IsStudent { get; set; }
   // Constructor to set default values
   public Person()
   {
       Name = "Unknown";
       Age = 0;
       IsStudent = false;
   }
}

3. Default Values with Auto-Properties (C# 6 and later)

Starting from C# 6, you can use auto-property initializers to set default values.

public class Person
{
   public string Name { get; set; } = "Unknown";
   public int Age { get; set; } = 0;
   public bool IsStudent { get; set; } = false;
}

4. Default Values with Backing Fields

You can also use backing fields to set default values.

public class Person
{
   private string _name = "Unknown";
   private int _age = 0;
   private bool _isStudent = false;
   public string Name
   {
       get { return _name; }
       set { _name = value; }
   }
   public int Age
   {
       get { return _age; }
       set { _age = value; }
   }
   public bool IsStudent
   {
       get { return _isStudent; }
       set { _isStudent = value; }
   }
}

Example Usage

Here’s how you can use the Person class with default values:

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main()
        {
            // Using the default constructor
            Person person1 = new Person();
            Console.WriteLine($"Name: {person1.Name}, Age: {person1.Age}, IsStudent: {person1.IsStudent}");

            // Setting properties explicitly
            Person person2 = new()
            {
                Name = "John",
                Age = 25,
                IsStudent = true
            };
            Console.WriteLine($"Name: {person2.Name}, Age: {person2.Age}, IsStudent: {person2.IsStudent}");
            Console.ReadLine();
        }
    }
}

Explanation

  1. Default Values in Field Initializers: Properties are initialized directly where they are declared using the = operator.
  2. Default Values in Constructors: Default values are set inside the constructor of the class. This ensures that all instances of the class will have the specified default values unless they are explicitly overridden.
  3. Default Values with Auto-Properties: This approach simplifies the syntax for setting default values for properties.
  4. Default Values with Backing Fields: This approach provides more control over the properties and allows for additional logic when getting or setting the property values.

These methods allow you to ensure that your properties have meaningful default values, reducing the chances of null or uninitialized values in your class instances.


Updated 10-Jun-2024
Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer

Leave Comment

Comments

Liked By